package com.stephen.bp.model;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

/**
 * @author qiuwenwei
 */
@Data
@TableName(value = "record")
public class Record {
    @TableId(type = IdType.INPUT)
    String roomId;
    String aUser;
    String bUser;
    String aResult;
    String bResult;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
    Date createTime;

    @TableField(exist = false)
    private final HashMap<Integer, String> stepMap = new HashMap<>();

    public void format(Room room) {
        String aResult = "战队名称: " + aUser + "\n";
        aResult += "选择顺序: "
                + room.aSelectTeam.get(0).getName() + setStep(1, room.aSelectTeam.get(0).getName()) + timeout(room.getATimeoutSelectTeam(), room.aSelectTeam.get(0).getName()) + ","
                + room.aSelectTeam.get(1).getName() + setStep(6, room.aSelectTeam.get(1).getName()) + timeout(room.getATimeoutSelectTeam(), room.aSelectTeam.get(1).getName()) + ","
                + room.aSelectTeam.get(2).getName() + setStep(7, room.aSelectTeam.get(2).getName()) + timeout(room.getATimeoutSelectTeam(), room.aSelectTeam.get(2).getName()) + "\n";
        aResult += "禁用顺序: " + room.aBanTeam.get(0).getName() + "4" + timeout(room.getATimeoutBanTeam(), room.aBanTeam.get(0).getName()) + "\n";
        aResult += "最终顺序: "
                + room.aFinalSelectTeam.get(0).getName() + step(room.aFinalSelectTeam.get(0).getName()) + timeout(room.getATimeoutFinalSelectTeam(), room.aFinalSelectTeam.get(0).getName()) + ","
                + room.aFinalSelectTeam.get(1).getName() + step(room.aFinalSelectTeam.get(1).getName()) + timeout(room.getATimeoutFinalSelectTeam(), room.aFinalSelectTeam.get(0).getName()) + ","
                + room.aFinalSelectTeam.get(2).getName() + step(room.aFinalSelectTeam.get(2).getName()) + timeout(room.getATimeoutFinalSelectTeam(), room.aFinalSelectTeam.get(0).getName()) + "\n";
        aResult += "未选: 无";
//                + room.canSelectTeam.get(0).getName();
        this.aResult = aResult;


        String bResult = "战队名称: " + bUser + "\n";
        bResult += "选择顺序: "
                + room.bSelectTeam.get(0).getName() + setStep(2, room.bSelectTeam.get(0).getName()) + timeout(room.getBTimeoutSelectTeam(), room.bSelectTeam.get(0).getName()) + ","
                + room.bSelectTeam.get(1).getName() + setStep(5, room.bSelectTeam.get(1).getName()) + timeout(room.getBTimeoutSelectTeam(), room.bSelectTeam.get(1).getName()) + ","
                + room.bSelectTeam.get(2).getName() + setStep(8, room.bSelectTeam.get(2).getName()) + timeout(room.getBTimeoutSelectTeam(), room.bSelectTeam.get(2).getName()) + "\n";

        bResult += "禁用顺序: " + room.bBanTeam.get(0).getName() + "3" + timeout(room.getBTimeoutBanTeam(), room.bBanTeam.get(0).getName()) + "\n";
        bResult += "最终顺序: "
                + room.bFinalSelectTeam.get(0).getName() + step(room.bFinalSelectTeam.get(0).getName()) + timeout(room.getBTimeoutFinalSelectTeam(), room.bFinalSelectTeam.get(0).getName()) + ","
                + room.bFinalSelectTeam.get(1).getName() + step(room.bFinalSelectTeam.get(1).getName()) + timeout(room.getBTimeoutFinalSelectTeam(), room.bFinalSelectTeam.get(1).getName()) + ","
                + room.bFinalSelectTeam.get(2).getName() + step(room.bFinalSelectTeam.get(2).getName()) + timeout(room.getBTimeoutFinalSelectTeam(), room.bFinalSelectTeam.get(2).getName()) + "\n";
        bResult += "未选: 无";
//                + room.canSelectTeam.get(1).getName();
        this.bResult = bResult;
    }

    private String setStep(int step, String name) {
        stepMap.put(step, name);
        return "";
    }

    private String step(String name) {
        for (Map.Entry<Integer, String> entry : stepMap.entrySet()) {
            if (entry.getValue().equals(name)) {
                return String.valueOf(entry.getKey());
            }
        }
        return "";
    }

    private String timeout(Set<String> set, String name) {
        return (set.contains(name) ? "(超时)" : "");
    }
}
